<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户资金明细
 * Class ConsumerFound
 * @author heyu
 */
class ConsumerFound extends MY_Controller {
    public $mBuy;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerBuyModel', 'mBuy', 'consumer');
	}

	/**
	 * 首页
	 */
	public function index(){
        $config = $this->mBuy->config;
		$data = array(
			'title' =>'分享收益',
			'status' => $config['status'],
		);
		$this->view('fund/consumerShare',$data,'datatable,validation,datepicker');
	}
    /**
     * 详情
     */
    public function details(){
        $config = $this->mBuy->config;
        $data = array(
            'title' =>'分享收益',
            'status' => $config['status'],
        );
        $this->view('fund/consumerFound',$data,'datatable,validation,datepicker');
    }
    /**
     * 列表
     */
    public function getShareList(){
        $this->loadModel('ConsumerUserModel', 'mConsumer', 'consumer');
        $this->loadModel('ConsumerPromotionModel', 'mPromotion', 'consumer');

        //分享记录
        $mobile = trim(get('mobile'));
        $where = '';
        if($mobile){
            $where .= ' AND `p`.`mobile` like "%'.$mobile.'%"';
        }
        $table = '(SELECT `p`.`id`, `p`.`mobile`, b.bonusCount, bl.amount
                FROM `consumer` `u`
                JOIN `consumer` `p` ON `p`.`id` = `u`.`parent_id`
                LEFT JOIN (SELECT count(id) as bonusCount,consumer_id from consumer_bonus_log where type = 1 GROUP BY consumer_id) `b` ON `b`.`consumer_id` = `p`.`id`
                LEFT JOIN (SELECT consumer_id,sum(amount) as amount from consumer_buy_log where type = 7 GROUP BY consumer_id) `bl` ON `bl`.`consumer_id` = `p`.`id`
                WHERE `p`.`status` > 0 '.$where.'
                GROUP BY `p`.`id` ORDER BY `p`.`id`) a';

        $this->db->select('a.*');
        $data = $this->mUser->pagination($table);

        if(!empty($data['data'])){
            foreach ($data['data'] as $k =>$v){
                $data['aaData'][$k][] = $v['mobile'];
                $data['aaData'][$k][] = $v['amount'];
                $data['aaData'][$k][] = $v['bonusCount'];

                $btnConfig = ' '.adminCreateBtn(array(
                        'label' => ' 查看',
                        'attr' => array(
                            'href' => getUrl('fund/consumerFound/details?consumer_id='.$v['id']),
                            'class' => 'btn btn-xs default blue-stripe',
                        ),
                        'tag' => 'a',
                        'permissions' => array(
                            'module' => 'fund',
                            'key' => 'ConsumerCash-status'
                        )
                    ));

                $data['aaData'][$k][] = $btnConfig;
            }
        }

        unset($data['data']);
        echo json_encode($data);
    }


	/**
	 * 列表
	 */
	public function getList(){

		$data = $this->mBuy->adminGetList();
		if(!empty($data['data'])){
			foreach ($data['data'] as $k =>$v){
				$data['aaData'][$k][] = $v['mobile'];
				$data['aaData'][$k][] = $v['child_mobile'];
				$data['aaData'][$k][] = $v['amount'];
				$data['aaData'][$k][] = $v['bonusCount'];
                $data['aaData'][$k][] = dateFormat($v['addtime']);
                $btnConfig = '';
				$data['aaData'][$k][] = $btnConfig;
			}
		}

		unset($data['data']);
		echo json_encode($data);
	}

	/**
	 * 获取状态
	 *
	 * @param	integer	$key
	 * @param	boolean	$show	是否显示
	 * @return	mixed
	 */
	public function getStatus($key = null, $show = false){

		$config = array(
            '1' => '失败',
            '2' => '成功',
            '3' => '申请中'
		);

		if($key !== null){

			if(isset($config[$key]) && ! $show){
				return $config[$key];
			}

			if(isset($config[$key]) && $show){

				if($key == 1){
					return '<span class="label label-danger">'.$config[$key].'</span>';
				}else if($key == 2){
					return '<span class="label label-success">'.$config[$key].'</span>';
				}else if($key == 3){
					return '<span class="label label-warning">'.$config[$key].'</span>';
				}
			}
			return '';
		}

		return $config;
	}


}
